package com.jd.finance.core.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jd.finance.core.pojo.dto.NoCheckLoanDTO;
import com.jd.finance.core.pojo.entity.LoanApply;
import com.jd.finance.core.service.LoanApplyService;
import com.jd.finance.core.service.UserService;
import com.jd.finance.core.service.impl.AdminUtilsServiceImpl;
import com.jd.finance.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;


/**
 * <p>
 * 贷款申请表 前端控制器
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-25
 */
@Slf4j
@Api(tags = "贷款申请（风控查询）")
@RestController
@RequestMapping("/admin/core/adminLoanApply")
public class AdminLoanApplyController {

    @Autowired
    private LoanApplyService loanApplyService;
    @Autowired
    private AdminUtilsServiceImpl adminUtilsService;

    @Autowired
    private UserService userService;

    @ApiOperation("获取所有第一轮通过的申请")
    @GetMapping("/getAllFirstAuditApply/{page}/{limit}")
    public BaseResult getAllFirstAuditApply(
            Integer uid,

            @ApiParam(value = "当前页", required = true)
            @PathVariable long page,

            @ApiParam(value = "每页记录", required = true)
            @PathVariable long limit) {

        if (uid == null) {
            log.error("获取所有第一轮通过的申请,uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsService.isRisk(uid)) {
            log.error("没有访问权限");
            return BaseResult.error().message("没有访问权限");
        }

        Page<LoanApply> pageParam = new Page<>(page, limit);

        IPage<LoanApply> pageModel = loanApplyService.listPage(pageParam);
        return BaseResult.ok().data("pageModel", pageModel);


    }

    @ApiOperation("根据贷款申请id，获取贷款申请详情(风控)")
    @GetMapping("/getAllApplyInfo")
    public BaseResult getAllApplyInfo(Integer uid, Integer loanAppId) {
        if (uid == null || loanAppId == null) {
            log.error("id为空C");
            return BaseResult.error().message("id为空C");
        }
        if (!adminUtilsService.isRisk(uid)) {
            log.error("无访问权限C");
            return BaseResult.error().message("无访问权限");
        }
        return loanApplyService.getAllApplyInfo(loanAppId);

    }

    @ApiOperation("获取未审核 贷款")
    @GetMapping("/getNoAuditLoan/{page}/{limit}/{uid}")
    public BaseResult getNoAuditLoan(

            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(value = "查询操作的uid", required = true)
            @PathVariable Integer uid) {

        /**
         * 判断 用户 是否是 审核员 身份
         */
        if (!adminUtilsService.isAuditor(uid)) {
            log.error("用户非审核员身份!");
            return BaseResult.error().message("用户非审核员身份!");
        }

        Page<NoCheckLoanDTO> pageParam = new Page<>(page, limit);
        Page<NoCheckLoanDTO> pageModel = loanApplyService.getNoAuditLoan(pageParam);
//        List<NoCheckLoanDTO> records = pageModel.getRecords();
//        records.stream().filter(bean -> {
//            Integer date=bean.getDaikuantime().get(ChronoField.MONTH_OF_YEAR);
//            String realDate=String.valueOf(date);
//            return true;
//        }).collect(Collectors.toList());
//        pageModel.setRecords(records);
        return BaseResult.ok().data("pageModel", pageModel);

    }

    @ApiOperation("根据贷款申请id，获取贷款申请详情(审核)")
    @GetMapping("/getApplyLoanInfoF")
    public BaseResult getApplyLoanInfoF(Integer uid, Integer loanAppId) {
        if (StringUtils.isAllBlank(String.valueOf(uid), String.valueOf(loanAppId))) {
            log.error("信息为空!");
            return BaseResult.error().message("信息为空!");
        }

        /**
         * 判断 用户 是否是 审核员 身份
         */
        if (!adminUtilsService.isAuditor(uid)) {
            log.error("用户非审核员身份!");
            return BaseResult.error().message("用户非审核员身份!");
        }
        return loanApplyService.getApplyLoanInfo(uid, loanAppId);
    }


    /**
     * 根据贷款申请id, 修改 审核状态
     *
     * @param uid
     * @param loanAppId
     * @param loanStatus
     * @param noPassInfo
     * @param auditorId
     * @param auditorName
     * @return
     */
    @ApiOperation("根据贷款申请id, 修改 审核状态")
    @PostMapping("/updateCheckStatus")
    public BaseResult updateCheckStatus(Integer uid, Integer loanAppId, String loanStatus,
                                        String noPassInfo, Integer auditorId, String auditorName, HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid), String.valueOf(loanAppId),
                loanStatus, noPassInfo, String.valueOf(auditorId), auditorName)) {
            log.error("审核信息为空!");
            return BaseResult.error().message("审核信息为空!");
        }

        /**
         * 判断 用户 是否是 审核员 身份
         */

        if (!adminUtilsService.isAuditor(uid)) {
            log.error("用户非审核员身份!");
            return BaseResult.error().message("用户非审核员身份!");
        }

        return loanApplyService.updateCheckStatus(uid, loanAppId, loanStatus, noPassInfo, auditorId, auditorName, request);
    }

    /**
     * 获取所有审核员信息
     */
    @ApiOperation("获取所有审核员信息")
    @GetMapping("/getAllAuditor")
    public BaseResult getAllAuditor(Integer uid) {

        if (uid == null) {
            log.error("管理员信息为空!");
            return BaseResult.error().message("管理员信息为空!");
        }

        if (!adminUtilsService.isAdmin(uid)) {
            log.error("用户非管理员身份!");
            return BaseResult.error().message("用户非管理员身份!");
        }
        return userService.getAllAuditor(uid);
    }


    @ApiOperation("根据贷款申请id，修改风控状态")
    @PostMapping("/updateRiskStatus")
    public BaseResult updateRiskStatus(Integer uid, Integer loanAppId, Integer riskId,
                                       String riskName, Integer isPass, String message,
                                       Integer isTop, BigDecimal topRate, BigDecimal agreeMoney,
                                       HttpServletRequest request) {

        if (uid == null || loanAppId == null || riskId == null) {
            log.error("传入id为空值C");
            return BaseResult.error().message("传入id为空值C");
        }
        if (StringUtils.isAllBlank(riskName, message)) {
            log.error("信息为空C");
            return BaseResult.error().message("信息为空C");
        }
        if (!adminUtilsService.isAdmin(uid)) {
            log.error("登录用户为非管理员C");
            return BaseResult.error().message("登录用户为非管理员C");
        }
        if (!adminUtilsService.isRisk(riskId)) {
            log.error("登录用户为非风控员C");
            return BaseResult.error().message("登录用户为非风控员C");
        }

        if (isPass >= 1) {
            return loanApplyService.passRisk(uid, loanAppId, riskId, riskName, isPass,
                    message, isTop, topRate, agreeMoney, request);
        } else {
            return loanApplyService.noPassRisk(uid, loanAppId, riskId,
                    riskName, isPass, message, request);

        }
    }

    @ApiOperation("获取所有风控员信息")
    @GetMapping("/getAllRiskManList")
    public BaseResult getAllRiskManList(Integer uid) {
        if (uid == null) {
            log.error("uid为空!");
            return BaseResult.error().message("uid为空!");
        }
        if (!adminUtilsService.isAdmin(uid)) {
            log.error("该用户无访问权限C");
            return BaseResult.error().message("该用户无访问权限");
        }

        return loanApplyService.getAllRiskManList();
    }


}